package com.bootdo.program.wallpaper.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.program.wallpaper.domain.*;
import com.bootdo.program.wallpaper.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 余额记录表
 *
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-10-30 21:59:22
 */

@Controller
@RequestMapping("/wallpaper/balanceRecord")
public class BalanceRecordController extends BaseController {
	@Autowired
	private BalanceRecordService balanceRecordService;


	@Autowired
	private BzAllWallpaperService bzAllWallpaperService;

	@Autowired
	private BzConfigService bzConfigService;

	@Autowired
	private BzDeductionRecordService bzDeductionRecordService;

	@GetMapping()
	@RequiresPermissions("wallpaper:balanceRecord:balanceRecord")
	String BalanceRecord(Model model){
		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
		return "wallpaper/balanceRecord/balanceRecord";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:balanceRecord:balanceRecord")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BalanceRecordDO> balanceRecordList = balanceRecordService.list(query);
		int total = balanceRecordService.count(query);
		PageUtils pageUtils = new PageUtils(balanceRecordList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("wallpaper:balanceRecord:add")
	String add(){
		return "wallpaper/balanceRecord/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:balanceRecord:edit")
	String edit(@PathVariable("id") Long id,Model model){
		BalanceRecordDO balanceRecord = balanceRecordService.get(id);
		model.addAttribute("balanceRecord", balanceRecord);
		return "wallpaper/balanceRecord/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wallpaper:balanceRecord:add")
	public R save( BalanceRecordDO balanceRecord){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		if(balanceRecordService.save(balanceRecord)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:balanceRecord:edit")
	public R update( BalanceRecordDO balanceRecord){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		balanceRecordService.update(balanceRecord);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:balanceRecord:remove")
	public R remove( Long id){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		BalanceRecordDO recordDO = balanceRecordService.get(id);

		if(balanceRecordService.remove(id)>0){
			if(recordDO.getSysData() == 0){
				//修改壁纸对应的数据进行修改
				BzAllWallpaperDO wallpaperDO = bzAllWallpaperService.get(recordDO.getWid().intValue());
				wallpaperDO.setDownCount(null != wallpaperDO.getDownCount() && wallpaperDO.getDownCount() >= 1? wallpaperDO.getDownCount() - 1 : 0);
				bzAllWallpaperService.update(wallpaperDO);

				//修改扣量信息
				BzDeductionRecordDO deductionRecordDO = bzDeductionRecordService.getByUserId(recordDO.getUserId(), recordDO.getCreateTime());
				deductionRecordDO.setDeCount(null == deductionRecordDO.getDeCount() ? 1 : deductionRecordDO.getDeCount() + 1);
				bzDeductionRecordService.updateDeCount(deductionRecordDO);
			}
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:balanceRecord:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		for (int i = 0; i < ids.length; i++) {
			BalanceRecordDO recordDO = balanceRecordService.get(ids[i]);
			if(recordDO.getSysData() == 0){
				//修改壁纸对应的数据进行修改
				BzAllWallpaperDO wallpaperDO = bzAllWallpaperService.get(recordDO.getWid().intValue());
				wallpaperDO.setDownCount(null != wallpaperDO.getDownCount() && wallpaperDO.getDownCount() >= 1? wallpaperDO.getDownCount() - 1 : 0);
				bzAllWallpaperService.update(wallpaperDO);

				//修改扣量信息
				BzDeductionRecordDO deductionRecordDO = bzDeductionRecordService.getByUserId(recordDO.getUserId(), recordDO.getCreateTime());
				deductionRecordDO.setDeCount(null == deductionRecordDO.getDeCount() ? 1 : deductionRecordDO.getDeCount() + 1);
				bzDeductionRecordService.updateDeCount(deductionRecordDO);
			}
		}

		balanceRecordService.batchRemove(ids);
		return R.ok();
	}


	@PostMapping("/copyData")
	@ResponseBody
	@RequiresPermissions("wallpaper:balanceRecord:add")
	public R copyData(@RequestParam("ids[]") Long[] ids){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		List<BzConfigDO> type = bzConfigService.getType("copy_data");

		int count = 100;
		if(type.size() > 0){
			count = Integer.valueOf(type.get(0).getValue()).intValue();
		}
		//添加结算单
		Date date = new Date();
		String dateStr = DateUtil.format(date, "yyyyMMddHHmmss");

		for (int i = 0; i < ids.length; i++) {
			BalanceRecordDO recordDO = balanceRecordService.get(ids[i]);
			for (int j = 0; j < count; j++) {
				recordDO.setCopyData(1);
				recordDO.setId(null);
				recordDO.setFlowNo(dateStr + j);
				balanceRecordService.save(recordDO);
			}
		}

		return R.ok();
	}

}
